package com.weex.app.module.tool;


import android.app.Activity;
import android.app.ProgressDialog;

import java.lang.ref.WeakReference;

public class LoadDialog {
    private static ProgressDialog dialog;
    private static WeakReference<Activity> mActivity;

    public static void show(final Activity activity) {
        if (activity == null) return;
        mActivity = new WeakReference<Activity>(activity);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!activity.isFinishing()) {
                    dialog = new ProgressDialog(activity);
                    //dialog.setTitle("");
                    dialog.setMessage("loading...");
                    dialog.setCancelable(true);
                    if (!dialog.isShowing()) {
                        dialog.show();
                    }
                }
            }
        });
    }

    public static void hide(Activity activity) {
        if (activity == null) {
            if (mActivity == null) {
                return;
            }
            activity = mActivity.get();
        }
        if (activity == null) return;

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
        });
    }
}
